<template>
  <div>
    <el-card>
      <template #header>
        <h4>修改密码</h4>
      </template>
      <el-form
        :model="changeForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="旧密码" prop="originalPass">
          <el-input
            type="password"
            v-model="changeForm.originalPass"
            autocomplete="off"
            @blur="original"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input
            type="password"
            v-model="changeForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="changeForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {
  validatePass,
  newValidatePass,
  validatePass2
} from '../../utils/validate.js'
import local from '../../utils/local.js'
import { editpwd, checkoldpwd } from '../../api/info.js'

export default {
  data() {
    return {
      changeForm: {
        originalPass: '',
        pass: '',
        checkPass: ''
      },
      rules: {
        originalPass: [{ validator: validatePass, trigger: 'blur' }],
        pass: [{ validator: newValidatePass(this), trigger: 'blur' }],
        checkPass: [{ validator: validatePass2(this), trigger: 'blur' }]
      },
      falg: false
    }
  },
  methods: {
    // 旧密码验证请求
    async original() {
      const { code } = await checkoldpwd({
        oldPwd: this.changeForm.originalPass
      })
      if (code === '00') {
        this.falg = true
      } else {
        this.$message.error('原密码错误')
        this.falg = false
      }
    },

    submitForm(formName) {
      if (this.falg) {
        this.$refs[formName].validate(async valid => {
          if (valid) {
            const { code, msg } = await editpwd({
              newPwd: this.changeForm.pass
            })
            console.log(code)
            if (code === 0) {
              this.$alert(msg, {
                confirmButtonText: '确定',
                callback: action => {
                  local.clear()
                  this.$router.push('/login')
                }
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      } else {
        this.$message.error('请先输入正确的原密码')
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-input__inner {
  width: 300px;
}
</style>
